package com.aivin.lint_lib_java.detector;

import com.aivin.lint_lib_java.JavaLog;
import com.android.tools.lint.detector.api.Category;
import com.android.tools.lint.detector.api.Detector;
import com.android.tools.lint.detector.api.Implementation;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.JavaContext;
import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.Severity;
import com.android.tools.lint.detector.api.TextFormat;
import com.intellij.psi.PsiMethod;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.uast.UCallExpression;
import org.jetbrains.uast.UExpression;

import java.util.Arrays;
import java.util.List;


/**
 *  字符串 硬编码 规范。 要用 在xml 配置好国家化。
 */
public class AVTextStringInCodeDetector extends Detector implements Detector.UastScanner {

    public static final Issue ISSUE = Issue.create(
            "Aivin_StringIssue",
            "不建议字符串硬编码...",
            "不要直接在代码中写入字符串",
            Category.CORRECTNESS, 9, Severity.ERROR,
            new Implementation(AVTextStringInCodeDetector.class, Scope.JAVA_FILE_SCOPE));


    @Override
    public List<String> getApplicableMethodNames() {
        return Arrays.asList("setText");
    }


    @Override
    public void visitMethodCall(@NotNull JavaContext context,
                                @NotNull UCallExpression node,
                                @NotNull PsiMethod method) {

        // 方法的参数个数
        int size  = node.getValueArguments().size() ;
        if(size !=1){
            return;
        }

        UExpression uExpression =node.getValueArguments().get(0);
        // 参数内容以 引号 " 开头
        if(! uExpression.asRenderString().startsWith("\"")){
            return;
        }

        context.report(ISSUE, node,
                context.getLocation(node),
                ISSUE.getBriefDescription(TextFormat.TEXT));

        JavaLog.showLog("检测到硬编码使用不规范");


    }
}